package com.example.springbootfastdfs.controller;

import com.example.springbootfastdfs.domain.ImageVo;
import com.example.springbootfastdfs.utils.Base64Util;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/upload")
public class UploadFile {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @PostMapping("imageUpload")
    public Map<String,Object> imageUpload(@RequestBody ImageVo imageVo) throws FileNotFoundException {
        String imageBase64 = imageVo.getBase64().replace("data:image/jpeg;base64,", "");
        File file = Base64Util.base64ConvertFile(imageBase64);
        StorePath storePath = this.fastFileStorageClient.uploadImageAndCrtThumbImage(new FileInputStream(file),file.length(),"jpg",null);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",200);
        hashMap.put("data","http://ip:8888/"+storePath.getFullPath());
        return hashMap;
    }
}
